package com.example.prison.service.impl;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.prison.entity.AreaManagement;
import com.example.prison.entity.AreaRecord;
import com.example.prison.entity.DefenceArea;
import com.example.prison.entity.vo.AreaManagementVO;
import com.example.prison.mapper.*;
import com.example.prison.service.AreaManagementService;
import com.example.prison.util.APIUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
* @author jiang
* @description 针对表【area_management】的数据库操作Service实现
* @createDate 2023-05-26 17:15:28
*/
@Service
public class AreaManagementServiceImpl extends ServiceImpl<AreaManagementMapper, AreaManagement>
        implements AreaManagementService {

    @Autowired
    private AreaManagementMapper areaManagementMapper;

    @Autowired
    private AlarmManagementMapper alarmManagementMapper;

    @Autowired
    private DefenceAreaMapper defenceAreaMapper;

    @Autowired
    private AreaRecordMapper areaRecordMapper;


    @Override
    public PageInfo<AreaManagement> get(int pageNumber, int pageSize,
                                        String defenseSection,String sentryPost,String defenseSectionPoint,String alarmStatus) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.get(defenseSection, defenseSectionPoint, sentryPost, alarmStatus));
    }

    @Override
    public List<AreaManagement> list(){
        return areaManagementMapper.list();
    }

    @Override
    public PageInfo<AreaManagementVO> getDefenceArea(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        return new PageInfo<>(areaManagementMapper.getDefenceArea());
    }

    @Override
    public PageInfo<AreaManagement> getAreaRealtimeState(int pageNumber, int pageSize) {
        PageHelper.startPage(pageNumber, pageSize);
        List<AreaManagement> areaList = areaManagementMapper.get("","","","");
//        Random r = new Random();
//        for (AreaManagement area : areaList) {
//            //查询设备信息
//            String[] b = {"正常", "短路", "断路", "触网", "关闭"};
//            area.setVoltage(String.valueOf(r.nextInt(2000)));
//            area.setCurrent(String.valueOf(r.nextInt(10)));
//            area.setAlarmStatus(b[r.nextInt(5)]);
////            area.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//            //更新实时数据
//            String alarmOne = alarmManagementMapper.getOneLevel("一级").getVoltage();
//            String alarmTwo = alarmManagementMapper.getOneLevel("二级").getVoltage();
//            String alarmThree = alarmManagementMapper.getOneLevel("三级").getVoltage();
//            area.setAlarmLevel(APIUtil.CountAlarmLevel(area.getVoltage(),area.getRatedVoltage(),alarmOne,alarmTwo,alarmThree));
//            areaManagementMapper.updateArea(area, area.getId());
//            //记录数据
//            AreaRecord record = new AreaRecord();
//            record.setDefenseSection(area.getDefenseSection());
//            record.setDefenseSectionPoint(area.getDefenseSectionPoint());
//            record.setTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
//            record.setVoltage(area.getVoltage());
//            record.setCurrent(area.getCurrent());
//            record.setRatedVoltage(area.getRatedVoltage());
//            record.setAlarmStatus(area.getAlarmStatus());
//            record.setDeviceNumber(area.getDeviceNumber());
//            record.setSentryPost(area.getSentryPost());
//            record.setSentryAddress(area.getSentryAddress());
//            record.setAlarmLevel(area.getAlarmLevel());
//            areaRecordMapper.add(record);
//        }
        return new PageInfo<>(areaList);
    }


    @Override
    public int add(AreaManagement area) {
        String defenseSection = area.getDefenseSection();
        String defenseSectionPoint = area.getDefenseSectionPoint();
        if (ObjectUtils.isNotEmpty(areaManagementMapper.getBySection(defenseSection))) {
            return -1;
        } else {
//            DefenceArea defenceArea = new DefenceArea();
//            defenceArea.setNumber(area.getDefenseSection());
//            defenceArea.setAddress(area.getDefenseAddress());
//            defenceArea.setStatus(area.getStatus());
//            defenceAreaMapper.add(defenceArea);
            area.setIsMark("未标记");
            area.setSetting("布防");
            areaManagementMapper.add(area);
            return 1;
        }
    }

    @Override
    public Integer delete(Integer[] ids) {
        for (Integer id : ids ) {
            if (ObjectUtils.isEmpty(areaManagementMapper.getById(id))) {
                return -1;
            }
            AreaManagement area = areaManagementMapper.getById(id);
//            if (ObjectUtils.isEmpty(defenceAreaMapper.getOneByNumber(area.getDefenseSection()))) {
//                areaManagementMapper.deleteAreaBySection(area.getDefenseSection());
//                return 0;
//            }
//            DefenceArea defenceArea = defenceAreaMapper.getOneByNumber(area.getDefenseSection());
//            defenceAreaMapper.deleteDefenceAreaById(defenceArea.getId());
            areaManagementMapper.deleteAreaBySection(area.getDefenseSection());
        }
        areaManagementMapper.deleteArea(ids);
        return 1;
    }

    @Override
    public int updateArea(AreaManagement area, Integer id) {
        if(ObjectUtils.isEmpty(areaManagementMapper.getById(id))) {
            return 2;
        }
        AreaManagement areaDTO = areaManagementMapper.getById(id);
        areaManagementMapper.updateArea(area, id);
        List<AreaManagement> areaList = areaManagementMapper.getByDefenseSection(area.getDefenseSection());
        if(areaList.size() > 1) {
            areaManagementMapper.updateArea(areaDTO, id);
            return -1;
        } else {
//            if(ObjectUtils.isEmpty(defenceAreaMapper.getOneByNumber(areaDTO.getDefenseSection()))) {
//                return 0;
//            }
//            DefenceArea defenceArea = defenceAreaMapper.getOneByNumber(areaDTO.getDefenseSection());
//            defenceArea.setNumber(area.getDefenseSection());
//            defenceArea.setAddress(area.getDefenseAddress());
//            defenceArea.setStatus(area.getStatus());
//            defenceAreaMapper.updateDefenceArea(defenceArea, defenceArea.getId());
            return 1;
        }
    }

    @Override
    public AreaManagement getById(Integer id) {
        return areaManagementMapper.getById(id);
    }

    @Override
    public int updateSetting(String setting, String moduleAddress) {
        return areaManagementMapper.updateSetting(setting, moduleAddress);
    }


}




